############
## Price of Peace Replication 0.R
## Last modified: March 19, 2019
## Josh Kertzer

## This file cleans the survey data and generates the necessary variables - run this file first!
## All of the following analyses were carried out using R version 3.5.2 GUI 1.7.0 El Capitan build (7612) on a 2017 iMac Pro running macOS Mojave 10.14.4

############

 
library(here) #Use to set working directory
library(ggplot2)
library(reshape2)
library(psych) 
library(stargazer)
 
#Functions to recode our variables
recode <- function(variable, reverse=FALSE, maxVal, binarize=FALSE, x=NA, x2){
	if (is.factor(variable)){variable <- as.numeric(variable)}
	if (missing(maxVal)){maxVal <- max(variable, na.rm=TRUE)}
	variable[variable > maxVal] <- NA
	if (reverse){
		temp <- variable
		for (j in 1:maxVal){
			temp[which(variable==j)] <- maxVal-j+1
		}
		return(temp)
	}
	if (binarize){
		if (is.na(x)){stop("Specify the value to dichotomize on")}
		temp <- variable
		if (missing(x2)){
			i <- which(variable == x)
			k <- which(variable < x | variable > x)
		}
		else{
			i <- which(variable >= x & variable <= x2)
			k <- which(variable < x | variable > x2)
		}
		temp[i] <- 1
		temp[k] <- 0
		return(temp)
	}
	return(variable)
}

#Rescales variable to range between 0-1
rescale <- function(x){
	return((x-min(x,na.rm=TRUE))/(max(x-min(x,na.rm=TRUE),na.rm=TRUE)))
}

##########
## Load raw datafiles
dat1 <- get(load("Iran Study 1.RData"))
dat2 <- get(load("Iran Study 2.RData"))

##########
## Clean study 1 data
#########

## Individual differences
dat1$Male <- recode(dat1$Q71, binarize=TRUE, x=1)
dat1$age <- 2015 - as.numeric(dat1$Q73)
dat1$White <- recode(dat1$Q75, binarize=TRUE, x=5)
dat1$pid1 <- rescale(recode(as.numeric(dat1$Q77), reverse=TRUE))
dat1$ideo1 <- rescale(recode(as.numeric(dat1$Q79), reverse=TRUE))
dat1$educ <- as.numeric(dat1$Q87)
dat1$church1 <- rescale(as.numeric(dat1$Q88))

#Feeling thermometers
dat1$china <- as.numeric(dat1$Q40_1)
dat1$nk <- as.numeric(dat1$Q40_2)
dat1$usa <- as.numeric(dat1$Q40_3)
dat1$iran <- as.numeric(dat1$Q40_4)
dat1$uk <- as.numeric(dat1$Q40_6)
dat1$obama <- as.numeric(dat1$Q89_1)
dat1$jeb <- as.numeric(dat1$Q89_2)
dat1$hillary <- as.numeric(dat1$Q89_3)

#Need for cognition
nfc1 <- as.numeric(dat1$Q52)
nfc2r <- recode(as.numeric(dat1$Q53), reverse=TRUE)
nfc3r <- recode(as.numeric(dat1$Q54), reverse=TRUE)
nfc4r <- recode(as.numeric(dat1$Q55), reverse=TRUE)
nfc5 <- as.numeric(dat1$Q56)
nfc6 <- as.numeric(dat1$Q57)
nfc7r <- recode(as.numeric(dat1$Q58), reverse=TRUE)
nfc8 <- as.numeric(dat1$Q59)
nfc9 <- as.numeric(dat1$Q60)

alpha(cbind(nfc1, nfc2r, nfc3r, nfc4r, nfc5, nfc6, nfc7r, nfc8, nfc9)) #a=0.92

dat1$cognition1 <- rescale(apply(cbind(nfc1, nfc2r, nfc3r, nfc4r, nfc5, nfc6, nfc7r, nfc8, nfc9),1,sum,na.rm=TRUE))

#Need for closure

c1r <- recode(as.numeric(dat1$Q89), reverse=TRUE)
c2 <- as.numeric(dat1$Q91)
c3 <- as.numeric(dat1$Q93)
c4r <- recode(as.numeric(dat1$Q95), reverse=TRUE)
c5r <- recode(as.numeric(dat1$Q97), reverse=TRUE)
c6r <- recode(as.numeric(dat1$Q99), reverse=TRUE)
c7r <- recode(as.numeric(dat1$Q101), reverse=TRUE)
c8 <- as.numeric(dat1$Q103)

alpha(cbind(c1r, c2, c3, c4r, c5r, c6r, c7r, c8)) #a=0.74

dat1$closure1 <- rescale(apply(cbind(c1r, c2, c3, c4r, c5r, c6r, c7r, c8), 1, sum, na.rm=TRUE))

#Political knowledge

k1_1 <- as.numeric(recode(dat1$Q27_1, binarize=TRUE, x=1))
k1_2 <- as.numeric(recode(dat1$Q27_2, binarize=TRUE, x=1))
k1_3 <- as.numeric(recode(dat1$Q27_3, binarize=TRUE, x=""))
k1_4 <- as.numeric(recode(dat1$Q27_4, binarize=TRUE, x=""))
k1_5 <- as.numeric(recode(dat1$Q27_5, binarize=TRUE, x=""))
k1 <- apply(cbind(k1_1, k1_2, k1_3, k1_4, k1_5), 1, mean, na.rm=TRUE)
k2 <- as.numeric(recode(dat1$Q28, binarize=TRUE, x=4))
k3 <- as.numeric(recode(dat1$Q29, binarize=TRUE, x=3))
k4 <- as.numeric(recode(dat1$Q30, binarize=TRUE, x=5))
dat1$know1 <- rescale(apply(cbind(k1, k2,k3,k4), 1, sum, na.rm=TRUE))

ci1 <- as.numeric(dat1$Q47.1)
ci2 <- as.numeric(dat1$Q48)
ci3 <- as.numeric(dat1$Q49)
ci4 <- as.numeric(dat1$Q50)
ci5 <- as.numeric(dat1$Q51)

alpha(cbind(ci1, ci2, ci3, ci4, ci5)) #a=0.82
dat1$ci1 <- rescale(apply(cbind(ci1, ci2, ci3, ci4, ci5), 1, sum, na.rm=TRUE))

mi1 <- as.numeric(dat1$Q52.1)
mi2 <- as.numeric(dat1$Q53.1)
mi3 <- as.numeric(dat1$Q55.1)
mi4r <- recode(as.numeric(dat1$Q56.1), reverse=TRUE)
mi5 <- as.numeric(dat1$Q57.1)

alpha(cbind(mi1, mi2, mi3, mi4r, mi5)) #a=0.78
dat1$mi1 <- rescale(apply(cbind(mi1, mi2, mi3, mi4r, mi5), 1, sum, na.rm=TRUE))

iso1 <- as.numeric(dat1$Q58.1)
iso2 <- as.numeric(dat1$Q59.1)
iso3r <- recode(as.numeric(dat1$Q60.1), reverse=TRUE)
iso4 <- as.numeric(dat1$Q61)
iso5 <- as.numeric(dat1$Q62)

alpha(cbind(iso1, iso2, iso3r, iso4, iso5)) #a=0.83

dat1$iso1 <- rescale(apply(cbind(iso1, iso2, iso3r, iso4, iso5), 1, sum, na.rm=TRUE))

####Treatments

#Fuel cycle
dat1$FuelCycle_Continue <- recode(dat1$FuelCycle, binarize=TRUE, x=1)
dat1$FuelCycle_Freeze <- recode(dat1$FuelCycle, binarize=TRUE, x=2)
dat1$FuelCycle_Dismantle <- recode(dat1$FuelCycle, binarize=TRUE, x=3)

#Inspection 
dat1$Inspection_NotAllow <- recode(dat1$Inspection, binarize=TRUE, x=1)
dat1$Inspection_NoMilitary <- recode(dat1$Inspection, binarize=TRUE, x=2)
dat1$Inspection_All <- recode(dat1$Inspection, binarize=TRUE, x=3)

#Dependent variables

dat1$trustworthy <- as.numeric(dat1$Q65)
dat1$dangerous <- as.numeric(dat1$Q67)
dat1$dealSupport <- as.numeric(dat1$Q69)
dat1$dealLikely <- as.numeric(dat1$Q79)

###########
## Clean study 2 data
###########

#Individual differences
dat2$Male <- recode(dat2$gender, binarize=TRUE, x=1)
dat2$age <- 2018 - as.numeric(dat2$birth.year)
dat2$White <- recode(dat2$race, binarize=TRUE, x=5)
dat2$pid1 <- rescale(recode(as.numeric(dat2$partyid), reverse=TRUE))
dat2$ideo1 <- rescale(recode(as.numeric(dat2$ideology), reverse=TRUE))
dat2$educ1 <- rescale(as.numeric(dat2$education))
dat2$church1 <- rescale(as.numeric(dat2$churchattendance))

#Feeling thermometers
dat2$china <- as.numeric(dat2$countryfeeling_1)
dat2$nk <- as.numeric(dat2$countryfeeling_2)
dat2$usa <- as.numeric(dat2$countryfeeling_3)
dat2$iran <- as.numeric(dat2$countryfeeling_4)
dat2$iran1 <- rescale(dat2$iran)
dat2$israel <- as.numeric(dat2$countryfeeling_6)
dat2$trump <- as.numeric(dat2$trumpfeeling_1)
dat2$trump1 <- rescale(dat2$trump)

#News closeness
dat2$news_syria <- as.numeric(dat2$attention_1)
dat2$news_iran <- as.numeric(dat2$attention_2)
dat2$news_nk <- as.numeric(dat2$attention_3)
dat2$news_china <- as.numeric(dat2$attention_4)
dat2$news_avg <- apply(cbind(dat2$news_syria, dat2$news_iran, dat2$news_nk, dat2$news_china),1,mean) #Average level of news attentiveness
dat2$news_iran_vs_avg <- dat2$news_iran - dat2$news_avg #Comparison of Iran to average news consumption
dat2$news_iran1 <- rescale(dat2$news_iran)

#Foreign policy orientations
#CI
ci1 <- as.numeric(dat2$CI1)
ci2 <- as.numeric(dat2$CI2)
ci3 <- as.numeric(dat2$CI3)
ci4 <- as.numeric(dat2$CI4)
ci5 <- as.numeric(dat2$CI5)

alpha(cbind(ci1, ci2, ci3, ci4, ci5)) #a=0.85
colnames(dat2)[grep("^CI1", colnames(dat2)):grep("^CI5", colnames(dat2))] <- c("CI1i", "CI2i", "CI3i", "CI4i", "CI5i") #Rename existing items to avoid confusion
dat2$ci1 <- rescale(apply(cbind(ci1, ci2, ci3, ci4, ci5), 1, sum, na.rm=TRUE))

#MI
mi1 <- as.numeric(dat2$MI1)
mi2 <- as.numeric(dat2$MI2)
mi3 <- as.numeric(dat2$MI3)
mi4r <- recode(as.numeric(dat2$MI4), reverse=TRUE)
mi5 <- as.numeric(dat2$MI5)

alpha(cbind(mi1, mi2, mi3, mi4r, mi5)) #a=0.77
colnames(dat2)[grep("^MI1", colnames(dat2)):grep("^MI5", colnames(dat2))] <- c("MI1i", "MI2i", "MI3i", "MI4i", "MI5i") #Rename existing items to avoid confusion
dat2$mi1 <- rescale(apply(cbind(mi1, mi2, mi3, mi4r, mi5), 1, sum, na.rm=TRUE))

#Isolationism
iso1 <- as.numeric(dat2$Iso1)
iso2 <- as.numeric(dat2$Iso2)
iso3r <- recode(as.numeric(dat2$Iso3), reverse=TRUE)
iso4 <- as.numeric(dat2$Iso4)
iso5 <- as.numeric(dat2$Iso5)

alpha(cbind(iso1, iso2, iso3r, iso4, iso5)) #a=0.84
colnames(dat2)[grep("^Iso1", colnames(dat2)):grep("^Iso5", colnames(dat2))] <- c("Iso1i", "Iso2i", "Iso3i", "Iso4i", "Iso5i")
dat2$iso1 <- rescale(apply(cbind(iso1, iso2, iso3r, iso4, iso5), 1, sum, na.rm=TRUE))

#Need for closure 
nfc2r <- recode(as.numeric(dat2$NFCC2), reverse=TRUE)
nfc3 <- as.numeric(dat2$NFCC3)
nfc4 <- as.numeric(dat2$NFCC4)
nfc5r <- recode(as.numeric(dat2$NFCC5), reverse=TRUE)
nfc6r <- recode(as.numeric(dat2$NFCC6), reverse=TRUE)
nfc7r <- recode(as.numeric(dat2$NFCC7), reverse=TRUE)
nfc8r <- recode(as.numeric(dat2$NFCC8), reverse=TRUE)
nfc9 <- as.numeric(dat2$NFCC9)

alpha(cbind(nfc2r, nfc3, nfc4, nfc5r, nfc6r, nfc7r, nfc8r, nfc9)) #a=0.76

dat2$closure1 <- rescale(apply(cbind(nfc2r, nfc3, nfc4, nfc5r, nfc6r, nfc7r, nfc8r, nfc9),1,sum,na.rm=TRUE))

#Political knowledge

k1_1 <- rep(0,nrow(dat2))
k1_1[grep("1",dat2$info1)] <- 1
k1_2 <- rep(0,nrow(dat2))
k1_2[grep("2",dat2$info1)] <- 1
k1_3 <- rep(1,nrow(dat2))
k1_3[grep("3",dat2$info1)] <- 0
k1_4 <- rep(1,nrow(dat2))
k1_4[grep("4",dat2$info1)] <- 0
k1_5 <- rep(1,nrow(dat2))
k1_5[grep("5",dat2$info1)] <- 0
k1 <- apply(cbind(k1_1, k1_2, k1_3, k1_4, k1_5), 1, mean, na.rm=TRUE)

k2 <- as.numeric(recode(dat2$info2, binarize=TRUE, x=4))
k3 <- as.numeric(recode(dat2$info3, binarize=TRUE, x=3))
k4 <- as.numeric(recode(dat2$info4, binarize=TRUE, x=5))
dat2$know1 <- rescale(apply(cbind(k1, k2,k3,k4), 1, sum, na.rm=TRUE))

#Prior
dat2$trustworthyPrior <- as.numeric(dat2$trustpre)

#Posterior
dat2$trustworthy <- as.numeric(dat2$trustpost_1)

#Posterior - prior
dat2$trustDiff <- dat2$trustworthy - dat2$trustworthyPrior
